//@ts-nocheck

import { defineStore } from "pinia";

import { storage } from "@/utils/Storage";


import { toast } from "@/plugins/toast";
import { useRouter } from 'vue-router'
import { useExamStore } from "@/store/modules/exam";

export interface IExamState {
    // 试卷信息
    paperInfo: any;
    // 试题列表
    questions: any[];
    currentQuestionId: string;
}

export const useExamResultStore = defineStore({
    id: "app-exam",
    state: (): IExamState => ({
        paperInfo: storage.get(EXAM_PAPER_INFO, {} as listData),
        questions: [],
        currentQuestionId: "",
    }),
    getters: {
        getPaperInfo(): listData {
            return this.paperInfo;
        },
        getPaperId(): string {
            return this.paperInfo.id;
        },
        // 获取试题列表
        getQuestions(): any[] {
            return this.questions;
        },
        // 获取当前题目
        getCurrentQuestion(): any {
            if (!this.currentQuestionId) {
                return {};
            }
            return this.questions.find((item) => item.id === this.currentQuestionId);
        },
        // 获取当前题目ID
        getCurrentQuestionId(): string {
            return this.currentQuestionId;
        },
        // 获取题目序号
        getCurrentQuestionIndex(): number {
            if (!this.currentQuestionId) {
                return 0;
            }
            return this.questions.findIndex(
                (item) => item.id === this.currentQuestionId
            );
        },
    },
    actions: {
        // 设置试卷信息
        setPaperInfo(info: listData) {
            this.paperInfo = info;
            storage.set(EXAM_PAPER_INFO, info);
        },
        // 切换题目
        setCurrentQuestionId(id: string) {
            this.currentQuestionId = id;
        },
        // 下一题
        nextQuestion() {
            const index = this.getCurrentQuestionIndex;
            this.setCurrentQuestionId(this.questions[index + 1].id);
        },
        // 上一题
        prevQuestion() {
            const index = this.getCurrentQuestionIndex;
            if (index > 0) {
                this.setCurrentQuestionId(this.questions[index - 1].id);
            } else {
                toast.warning("已经是第一题了");
            }
        },

        // init
        init() {

        },
    },
});
